package com.bindapp.model;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

import com.bindapp.BR;
import com.bindapp.R;
import com.bindapp.adapter.EzItemAdapter;
import com.bindapp.databinding.ActivityEzlistBinding;
import com.bindapp.http.PunFuncRequest;
import com.bindapp.listener.HttpListener;
import com.bindapp.listener.LoadStatusListener;
import com.bindapp.listener.RecyclerItemClick;
import com.bindapp.listener.RefreshListener;
import com.bindapp.util.RefreshUtil;
import com.bindapp.util.Static;
import com.bindapp.widget.RVItemDecoration;
import com.videogo.openapi.bean.EZDeviceInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tom055 on 2018/6/24.
 */

public class EzlistModel extends BaseModel {
    private ActivityEzlistBinding binding;
    private LoadStatusListener listener;
    private EzItemAdapter adapter;
    private List<Object> datalist=new ArrayList<Object>();//EZDeviceInfo
    private int page=0;

    public EzlistModel(Context context,LoadStatusListener listener){
        super(context);
        this.listener=listener;
        adapter=new EzItemAdapter(datalist,R.layout.item_ez, BR.itemdata,new ItemClick());
        binding= DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.activity_ezlist,null,false);
        binding.recycleview.setAdapter(adapter);
        binding.recycleview.addItemDecoration(new RVItemDecoration(30));
        binding.recycleview.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
        refreshUtil = new RefreshUtil(context, binding.ptrLayout, new Refresh());
        refreshUtil.initRefreshView();
    }



    public void requestToken(){
        listener.hide(true,"");
        PunFuncRequest.requestEzToken(context, new HttpListener() {
            @Override
            public void success(Object obj) {
                requestData();
            }

            @Override
            public void fail(String msg) {
                Static.showToast(context,"ezToken获取失败");

            }
        });
    }

    public void requestData(){
        PunFuncRequest.getEzList(Static.LOAD_MY_DEVICE, page, new HttpListener() {
            @Override
            public void success(Object obj) {
                binding.ptrLayout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        binding.ptrLayout.refreshComplete();
                    }
                }, 20);
                if(page == 0){
                    datalist.clear();
                }
                datalist.addAll((List<EZDeviceInfo>)obj);
                Log.e("Mytext","count"+datalist.size());
                if(page==0&&datalist.size()==0){
                    listener.hide(false,"暂无数据");
                }else if(obj==null||((List<EZDeviceInfo>)obj).size() < Static.rows){
                    Static.showToast(context,"暂无更多数据");
                    adapter.notifyDataSetChanged();
                    listener.show();
                }else{
                    adapter.notifyDataSetChanged();
                    listener.show();
                }

            }

            @Override
            public void fail(String msg) {
                binding.ptrLayout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        binding.ptrLayout.refreshComplete();
                    }
                }, 20);
                Static.showToast(context,msg);
                listener.hide(false,msg);
            }
        });
    }

    class ItemClick implements RecyclerItemClick {

        @Override
        public void onItemClick(int position) {

        }

        @Override
        public void onItemLongClick(int position) {

        }

        @Override
        public void onMoreClick(View view,int position) {

        }
    }


    class Refresh implements RefreshListener{

        @Override
        public void refresh() {
            page = 0;
            requestData();
        }

        @Override
        public void loadmore() {
            page++;
            requestData();
        }
    }

    @Override
    public ActivityEzlistBinding getBinding() {
        return binding;
    }
}
